package io.jft.doll.ui.home

import android.os.Bundle
import android.support.v4.widget.DrawerLayout
import android.view.Gravity
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.g.base.extend.setTimeout
import com.g.base.extend.toast
import com.g.base.ui.BaseActivity
import io.jft.doll.R
import io.jft.doll.databinding.ActivityHomeBinding
import io.jft.doll.router.RouterPage

/**
 * Created by G on 2017/11/13 0013.
 */

@Route(path = RouterPage.HomeActivity)
class HomeActivity : BaseActivity<ActivityHomeBinding>() {
    override var isFullScreen: Boolean = true
    override var isDefaultAnim: Boolean = false
    override fun supportSlideBack(): Boolean = false

    val drawerLayout: DrawerLayout by lazy { contentView.drawerLayout }
    private var lastBackPressTime = 0L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        ARouter.getInstance().build(RouterPage.LaunchActivity).navigation(this)
        setTimeout {
            setupView()
            showContentView()
        }
    }

    override fun onStop() {
        super.onStop()
        if (drawerLayout.isDrawerOpen(Gravity.START))
            drawerLayout.closeDrawer(Gravity.START, false)
    }

    private fun setupView() {
        val beginTransaction = supportFragmentManager.beginTransaction()
        beginTransaction.replace(R.id.content, HomeContentFragment())
        beginTransaction.replace(R.id.drawerMenu, HomeDrawerMenuFragment())
        beginTransaction.commitAllowingStateLoss()
    }

    override fun onBackPressed() {
        if (drawerLayout.isDrawerOpen(Gravity.START)) {
            drawerLayout.closeDrawer(Gravity.START)
            return
        }

        val currentTimeMillis = System.currentTimeMillis()
        if (currentTimeMillis - lastBackPressTime > 2000L) {
            toast("再按一次 退出程序")
        } else {
            super.onBackPressed()
        }
        lastBackPressTime = currentTimeMillis
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(0, 0)
    }
}